package com.lightheart.sphr.xiaobinggan.recyclerview;

import android.graphics.Canvas;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.lightheart.sphr.xiaobinggan.R;
import com.lightheart.sphr.xiaobinggan.base.BaseActivity;
import com.lightheart.sphr.xiaobinggan.timeselection.uitls.CustomToastUtils;

import java.util.ArrayList;

import butterknife.BindView;

/****
 * ItemTouchHelper的实践
 * https://www.jianshu.com/p/cb0351343ba1
 * */
public class BaseRecyclerViewActivity extends BaseActivity {

    @BindView(R.id.common_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.bt_sub)
    Button mSub;
    @BindView(R.id.common_toolbar_title_tv)
    TextView mTitleTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private ItemDraggableAdapter itemDraggableAdapter;
    public ArrayList arrayList = new ArrayList<String>();
    private boolean isTuo = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_recycler_view;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, mTitleTv, mSub, R.string.recyclerView, false, 0);

        for (int i = 0; i < 88; i++) {
            arrayList.add("我是小饼干呀" + i);
        }

        itemDraggableAdapter = new ItemDraggableAdapter(R.layout.recycler_item, arrayList);
        itemDraggableAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.setAdapter(itemDraggableAdapter);

        ItemDragAndSwipeCallBack itemDragAndSwipeCallback = new ItemDragAndSwipeCallBack(itemDraggableAdapter);//用于控制前面两个不让其移动
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);

        // 开启所有item的拖拽
//        itemDraggableAdapter.enableDragItem(itemTouchHelper, R.id.view_list_item_ll, true);
        itemDraggableAdapter.setOnItemDragListener(onItemDragListener);


        itemDraggableAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                if (position > 1) {
                    //开启单个item的拖拽
                    itemTouchHelper.startDrag(recyclerView.findViewHolderForAdapterPosition(position));
                }
                return false;
            }
        });

        // 开启滑动删除
        itemDraggableAdapter.enableSwipeItem();
        itemDraggableAdapter.setOnItemSwipeListener(onItemSwipeListener);
    }


    OnItemDragListener onItemDragListener = new OnItemDragListener() {
        @Override
        public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
            CustomToastUtils.INSTANCE.imgToast("你在拖拽第" + (pos + 1) + "个位置的item哦！", "666666");
        }

        @Override
        public void onItemDragMoving(RecyclerView.ViewHolder source, int from,
                                     RecyclerView.ViewHolder target, int to) {

        }

        @Override
        public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
            CustomToastUtils.INSTANCE.imgToast("拖拽到了第" + (pos + 1) + "个位置哦！", "666666");
        }
    };


    OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
            CustomToastUtils.INSTANCE.imgToast("继续向左滑动即可删除第" + (pos + 1) + "个位置的item", "666666");

        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {

        }

        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            CustomToastUtils.INSTANCE.imgToast("删除了第" + (pos + 1) + "个位置的item哦", "666666");
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder,
                                      float dX, float dY, boolean isCurrentlyActive) {

        }
    };

    @Override
    protected boolean showHomeAsUp() {
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(R.anim.slide_in_down, R.anim.slide_out_down);
        }
        return false;
    }
}